package com.liujg.future.api;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.liujg.future.config.MinioConfig;
import com.liujg.future.model.resp.BaseResult;
import com.liujg.future.util.MinioClientUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @date: 2022/5/21 10:33
 * @Description minio 文件处理（上传，下载，获取文件地址等）
 */
@Slf4j
@RestController
@CrossOrigin //处理跨域请求
@RequestMapping("/minioFile")
public class MinioFileController {

    @Resource
    private MinioClientUtils minioClientUtils;

    @Resource
    private MinioConfig minioConfig;


    @PostMapping(value = "/uploadFile")
    public BaseResult<String> uploadFile(@RequestParam("files") List<MultipartFile> files) {
        log.info(files.toString());
        if (CollectionUtils.isEmpty(files)) {

            return BaseResult.fail("未选择文件！",null);
        }

        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
//            获取文件拓展名
            String extName = FileUtil.extName(originalFilename);
            log.info("文件拓展名:" + extName);
//            生成新的文件名，存入到minio
            String minioFileName = "uploads/"+  originalFilename;
            String contentType = file.getContentType();
            log.info("文件mime:{}", contentType);
//            返回文件大小,单位字节
            long size = file.getSize();
            log.info("文件大小：" + size);
            try {
                String bucketName = minioConfig.getBucketName()  ;
                minioClientUtils.putObject(bucketName, file,  minioFileName);
                String fileUrl = minioClientUtils.getObjectUrl(bucketName, minioFileName);


                Map<String, Object> map = new HashMap<>();
                map.put("originalFilename", originalFilename);
                map.put("extName", extName);
                map.put("minioFileName", minioFileName);
                map.put("size", size);
                map.put("contentType", contentType);
                map.put("fileUrl", fileUrl);

                return new BaseResult<>(JSON.toJSONString(map));
            } catch (Exception e) {
                log.error("上传文件出错:{}", e);
                return new BaseResult<>("上传文件出错");

            }
        }
        return BaseResult.fail("上传文件出错",null);

    }

    /**
     * 删除文件
     *
     * @param minioFileName 文件名称
     * @return
     */
    @RequestMapping(value = "/deleteMinioFile", method = RequestMethod.GET)
    public BaseResult<String> deleteMinioFile(@RequestParam String minioFileName) {
        try {
            String bucketName = minioConfig.getBucketName();
            minioClientUtils.removeObject(bucketName, minioFileName);

        } catch (Exception e) {
            log.error("删除文件失败！");
            return new BaseResult<>("删除文件失败！");
        }
        return new BaseResult<>("删除文件成功");
    }
}
